//
//  DSPNewFeatureViewController.swift
//  SWweibo
//
//  Created by 杜守鹏 on 15/12/31.
//  Copyright © 2015年 杜守鹏. All rights reserved.
//

import UIKit
import SnapKit
class DSPNewFeatureViewController: UIViewController,UIScrollViewDelegate {
//MARK:懒加载 scrollView
  private  lazy var newFeatureScrollView:UIScrollView = {
        let scrollView = UIScrollView()
        
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.backgroundColor = UIColor.grayColor()
        scrollView.pagingEnabled = true
        scrollView.bounces = false
    scrollView.delegate = self
        return scrollView
    }()
    
    //MARK:懒加载 pageController
    lazy var pageController: UIPageControl = {
        let pageControl = UIPageControl()
        pageControl.currentPageIndicatorTintColor = UIColor.orangeColor()
        pageControl.pageIndicatorTintColor = UIColor.purpleColor()
        
        return pageControl
    }()
    
    //MARK:懒加载 enterButton
    lazy var enterButton: UIButton = {
       let button = UIButton()
        button.addTarget(self, action: "enterButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        
        button.setTitle("进入微博", forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(15)

        return button
    }()
    
    //MARK:懒加载  sharedButton
    lazy var sharedButton: UIButton = {
       let button = UIButton()
        button.addTarget(self, action: "sharedButtonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        // 设置不同状态的图片
        button.setImage(UIImage(named: "new_feature_share_false"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "new_feature_share_true"), forState: UIControlState.Selected)
        
        // 设置文字以及文字的属性
        button.setTitle("分享到微博", forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        
        // 调整边距
        button.titleEdgeInsets = UIEdgeInsetsMake(0, 5, 0, -5)

        return button
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setUpUI()
    }
//MARK:点击分享
@objc private func sharedButtonClick(button:UIButton){
        print("点击分享")
        button.selected = !button.selected
    }
    
//MARK:enterButtonClick
  @objc private func enterButtonClick(){
        print("进入微博")
    let vc = DSPWelcomeController()
    self.presentViewController(vc, animated: true , completion: nil)
    }
//MARK:设置ui
    func setUpUI(){
    //MARK - 设置scrollView
    view.addSubview(newFeatureScrollView)
        newFeatureScrollView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(view)
        }
        //设置图片
        let count = 4
        for i in 0..<count{
            
            let imageView = UIImageView(image: UIImage(named: "new_feature_\(i + 1)"))
            
            imageView.frame = CGRect(x: CGFloat(i) * SCREENWIDTH , y: 0, width: SCREENWIDTH, height: SCREENHIGHT)
            
            if i == count - 1{
              setupLastPage(imageView)
        }
            newFeatureScrollView.addSubview(imageView)
        //设置滚动范围
        newFeatureScrollView.contentSize = CGSize(width: CGFloat(count) * SCREENWIDTH, height: 0)
        
        //MARK- 设置pageController
         view.addSubview(pageController)
        pageController.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view)
            make.centerY.equalTo(view.snp_bottom).offset(-100)
        
        }
        pageController.numberOfPages = count
        }
    }
    //设置最后一页
    private func setupLastPage(imageView: UIImageView) {
        // 开启交互
        imageView.userInteractionEnabled = true
        imageView.addSubview(enterButton)
        imageView.addSubview(sharedButton)
        
        // 添加约束
        enterButton.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(imageView.snp_centerX)
            make.centerY.equalTo(imageView.snp_bottom).offset(-150)
        }
        
        sharedButton.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(enterButton)
            make.bottom.equalTo(enterButton.snp_top).offset(-20)
        }
    }

//MARK: 监听滚动，设置pageController
    func scrollViewDidScroll(scrollView: UIScrollView) {
           pageController.currentPage = Int(scrollView.contentOffset.x / scrollView.frame.width + 0.5 )
    }
    
   deinit{
        print("newfeature bye")
    }
}